
TheClient = 
{
    --The main game object
    myGame,
    --The audio engine
    myAudio,
    Print =     function(string)
                    TheEngine.Print(string)
                end,
    musicID,
    positionMusicID,
    positionNodeID,
    delayID = 
    {
        -1,
        -1,
        -1,
        -1
    },
    positionedDelayID = 
    {
        -1,
        -1,
        -1,
        -1
    },
    LoadFullTextures = function()
                        TheClient.Print("Loading Textures...0%...\n")
                        local path = "Textures/"
                        TheEngine.engine:LLoadTexture(path, "car.jpg")
                        TheEngine.engine:LLoadTexture(path, "policeCar.jpg")
                        TheEngine.engine:LLoadTexture(path, "cube.jpg")
                        TheEngine.engine:LLoadTexture(path, "tyreTexture.png")                        
                        --City
                        path = "Meshes/Block_0/"
                        TheEngine.engine:LLoadTexture(path, "concre0.jpg")
                        TheEngine.engine:LLoadTexture(path, "curb_d0.jpg")
                        TheEngine.engine:LLoadTexture(path, "dirtma3.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_021.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_030.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_060.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_070.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_081.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_101.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_110.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_121.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_170.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_180.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_191.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_261.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_270.jpg")
                        TheEngine.engine:LLoadTexture(path, "fac_291.jpg")
                        TheEngine.engine:LLoadTexture(path, "flatro1.jpg")
                        TheEngine.engine:LLoadTexture(path, "flatro2.jpg")
                        TheEngine.engine:LLoadTexture(path, "flatro3.jpg")
                        TheClient.Print("50%...")
                        TheEngine.engine:LLoadTexture(path, "flatro4.jpg")
                        TheEngine.engine:LLoadTexture(path, "flatro5.jpg")
                        TheEngine.engine:LLoadTexture(path, "flatro6.jpg")
                        TheEngine.engine:LLoadTexture(path, "flatro7.jpg")
                        TheEngine.engine:LLoadTexture(path, "flatro8.jpg")
                        TheEngine.engine:LLoadTexture(path, "flatro9.jpg")
                        TheEngine.engine:LLoadTexture(path, "parkin0.jpg")
                        TheEngine.engine:LLoadTexture(path, "parkin2.jpg")
                        TheEngine.engine:LLoadTexture(path, "parkin3.jpg")
                        TheEngine.engine:LLoadTexture(path, "parkin4.jpg")
                        TheEngine.engine:LLoadTexture(path, "paveme0.jpg")
                        TheEngine.engine:LLoadTexture(path, "road_d0.jpg")
                        TheEngine.engine:LLoadTexture(path, "roofti3.jpg")
                        TheEngine.engine:LLoadTexture(path, "roofti6.jpg")
                        TheEngine.engine:LLoadTexture(path, "yard_10.jpg")
                        TheEngine.engine:LLoadTexture(path, "yard_20.jpg")
                        TheEngine.engine:LLoadTexture(path, "yard_60.jpg")
                        TheEngine.engine:LLoadTexture(path, "yard_80.jpg")
                        TheEngine.engine:LLoadTexture(path, "yard_90.jpg")
                        
                        --finished
                        TheClient.Print("Loading Textures...complete")
                        TheClient.myGame:LUpdateTextures()
                        end,

    LoadCity =  function()
                        TheClient.Print("Loading City...0%")
                        local path = "Block_0/"
                        TheClient.myGame:LLoadBlock(path, "building_00", true)
                        TheClient.myGame:LLoadBlock(path, "building_01", true)
                        TheClient.myGame:LLoadBlock(path, "building_02", true)
                        TheClient.myGame:LLoadBlock(path, "building_03", true)
                        TheClient.Print("...25%...")
                        TheClient.myGame:LLoadBlock(path, "building_04", true)
                        TheClient.myGame:LLoadBlock(path, "building_05", true)
                        TheClient.myGame:LLoadBlock(path, "building_06", true)
                        
                        TheClient.myGame:LLoadBlock(path, "building_08", true)
                        TheClient.Print("...50%...")
                        TheClient.myGame:LLoadBlock(path, "building_09", true)
                        TheClient.myGame:LLoadBlock(path, "building_10", true)
                        TheClient.myGame:LLoadBlock(path, "building_11", true)
                        TheClient.myGame:LLoadBlock(path, "building_12", true)
                        TheClient.myGame:LLoadBlock(path, "building_13", true)
                        TheClient.Print("...75%...")
                        TheClient.myGame:LLoadBlock(path, "building_14", true)
                        TheClient.myGame:LLoadBlock(path, "building_15", true)
                        TheClient.myGame:LLoadBlock(path, "building_16", true)
                        TheClient.myGame:LLoadBlock(path, "building_17", true)
                        TheClient.Print("Loading City...100%")
                    end,

    LoadTestLevel = function()
                        --make a plane
                        TheClient.Print("Creating Plane...")
                        local length = 512
                        TheClient.myGame:LCreatePlaneActor(0,1,0, 0.01, length, -0.01,length)
                        --make a couple of spheres
                        local numSpheres = 10
                        local angle = 0
                        local inc = 360 / numSpheres
                        local radius = 50
                        TheClient.Print("Creating Spheres...")
                        for index = 1, numSpheres, 1 do
                            TheClient.myGame:LCreateSphereActor(7,math.cos(angle*math.pi/180)*radius,-1,
                                                                math.sin(angle*math.pi/180)*radius)
                            angle = angle + inc
                            radius = radius + 15
                        end
                        --make some walls!
                        TheClient.Print("Creating Walls...")
                        TheClient.myGame:LCreateWallActor(length, 5, length,
                                                        length, 5, -length,2)
                        TheClient.myGame:LCreateWallActor(-length, 5, -length,
                                                        -length, 5,  length,2)
                        TheClient.myGame:LCreateWallActor(length, 5, length,
                                                       -length, 5,  length,2)
                        TheClient.myGame:LCreateWallActor(-length, 5, -length,
                                                        length, 5,  -length,2)
                        TheClient.Print("Test level created...")
                    end,

    LoadTextures = function()
                        --Default is allready loaded in
                        --TheClient.LoadFullTextures()
                        local path = "Textures/"
                        TheEngine.engine:LLoadTexture(path, "audioDebug.png")
                        TheEngine.engine:LLoadTexture(path, "cubemap.png")
                        TheEngine.engine:LLoadTexture(path, "gordon.jpg")
                        TheEngine.engine:LLoadTexture(path, "noise.png")
                    end,

    LoadSharedMeshes = function()
                            TheClient.Print("Loading Meshes...")
                            local path = "Meshes/"
                            TheEngine.engine:LLoadSharedMesh(path, "bunny")
                            TheEngine.engine:LLoadSharedMesh(path, "teapot")
                            TheEngine.engine:LLoadSharedMesh(path, "tyreMesh")
                            TheEngine.engine:LLoadSharedMesh(path, "car")
                            TheEngine.engine:LLoadSharedMesh(path, "car_physics")
                            TheEngine.engine:LLoadSharedMesh(path, "teapot_physics")
                            TheEngine.engine:LLoadSharedMesh(path, "bunny_physics")
                            TheEngine.engine:LLoadSharedMesh(path, "unitSphere")
                            TheEngine.engine:LLoadSharedMesh(path, "unit_hemisphere")
                            TheClient.Print("Loading Meshes...Complete")
                        end,

    Reset =         function()
                        TheClient.Print("Game Reseting...")
                        local path = "Block_0/"
                        TheClient.myGame:LLoadBlock(path, "floor", false)
                        TheClient.myGame:LLoadBlock(path, "building_07", true)
                        TheClient.LoadCity()
                        TheClient.Print("Reset Complete")
                    end,

    InitialiseGame =    function()
                        TheClient.Print("Initialising Game Components...")
                        TheClient.Print("Creating Buffers...")
                        TheClient.Print("Connecting Broadphase...")
                        TheClient.Print("Compiling Shaders...")
                        TheClient.Print("Parsing Scripts...")
                        TheClient.LoadTextures()
                        --populate0
                        TheClient.Print("Loading Resources...")
                        TheClient.LoadSharedMeshes()
                        TheClient.Print("Creating Sky dome...")
                        --the 6 textures for the cubemap
                        path = "Textures/SkyBox2/"
                        TheEngine.engine:LLoadTexture(path, "box_floor.png")
                        TheEngine.engine:LLoadTexture(path, "box_top.png")
                        TheEngine.engine:LLoadTexture(path, "box_0.png")
                        TheEngine.engine:LLoadTexture(path, "box_1.png")
                        TheEngine.engine:LLoadTexture(path, "box_2.png")
                        TheEngine.engine:LLoadTexture(path, "box_3.png")
                        TheClient.myGame:LInitialiseSkyDome(0,128,0, 1024)

                        TheClient.myGame:AddLuaCommand("TheClient.PlayMusic()", "Plays some 1D Music")
                        TheClient.myGame:AddLuaCommand("TheClient.StopMusic()", "Stops the 1D Music")
                        TheClient.myGame:AddLuaCommand("TheClient.ReverbMusicOn()", "Adds reverb to the 1D music")
                        TheClient.myGame:AddLuaCommand("TheClient.ReverbMusicOff()", "Turns the 1D music reverb off")
                        TheClient.myGame:AddLuaCommand("TheClient.PositionMusic()", "Positions The Music at 0,25,0")
                        TheClient.myGame:AddLuaCommand("TheClient.StopPositionMusic()", "Stops the 3D music")
                        TheClient.myGame:AddLuaCommand("TheClient.Create3DSound()", "Creates an explosion near the building")
                        TheClient.myGame:AddLuaCommand("TheClient.Play3DSoundSource()", "Plays the plain WAV file in the explosion")
                        TheClient.myGame:AddLuaCommand("TheClient.myAudio:LStartAudio()", "Enables Audio")
                        TheClient.myGame:AddLuaCommand("TheClient.myAudio:LPauseAudio()", "Disables Audio")
                        TheClient.myGame:AddLuaCommand("TheClient.StopAllMusic()", "Clears all nodes")
                        TheClient.myGame:AddLuaCommand("TheClient.LoadCity()", "Loads the full city")
                        TheClient.myGame:AddLuaCommand("TheClient.LoadFullTextures()", "Warning: Slow! Loads all textures")

                        TheClient.Print("Game Started!")
                    end,
    Play3DSoundSource = function()
                            TheClient.myAudio:LCreateWAVNode("shot.wav", false, 0.01, 0.1)
                            end,
    Create3DSound     =     function()
                               TheClient.myAudio:LCreate3DSound("shot.wav",-40,75,-50, 1000)
                        end,
    StopPositionMusic =     function()
                                 TheClient.myAudio:LStopNode(TheClient.positionMusicID)
                                 TheClient.positionMusicID = -1
                                 TheClient.positionNodeID = -1
                        end,
    PositionMusic = function()
                            if TheClient.positionMusicID == -1 then
                                TheClient.positionMusicID = TheClient.myAudio:LCreateWAVNode("Play.wav", true, 1.3, 0.5)
                            end
                            if TheClient.positionMusicID > -1 and TheClient.positionNodeID == -1 then
                                TheClient.positionNodeID = TheClient.myAudio:LCreatePositionNode(TheClient.positionMusicID, 100, true, 0, 25, 0)
                            end
                        end,
    StopAllMusic =  function()
                                TheClient.myAudio:LStopAll()
                                TheClient.musicID = -1
                                TheClient.delayID[1] = -1
                                TheClient.delayID[2] = -1
                                TheClient.delayID[3] = -1
                                TheClient.delayID[4] = -1
                            end,
    PlayMusic       = function()
                            if TheClient.musicID == -1 then
                                TheClient.musicID = TheClient.myAudio:LCreateWAVNode("Play.wav", true, 1.3, 0.5)
                            end
                    end,
    StopMusic = function()
                            TheClient.myAudio:LStopNode(TheClient.musicID)
                            TheClient.musicID = -1
                    end,
    ReverbMusicOn     = function()
                            if TheClient.musicID > -1 then
                                TheClient.delayID[1] = TheClient.myAudio:LCreateDelayNode(TheClient.musicID, 0.2)
                                TheClient.delayID[2] = TheClient.myAudio:LCreateDelayNode(TheClient.musicID, 0.25)
                                TheClient.delayID[3] = TheClient.myAudio:LCreateDelayNode(TheClient.musicID, 0.15)
                                TheClient.delayID[4] = TheClient.myAudio:LCreateDelayNode(TheClient.musicID, 0.3)
                            end
                        end,
    ReverbMusicOff   = function()
                            if TheClient.musicID > -1 then
                                TheClient.myAudio:LStopNode(TheClient.delayID[1])
                                TheClient.myAudio:LStopNode(TheClient.delayID[2])
                                TheClient.myAudio:LStopNode(TheClient.delayID[3])
                                TheClient.myAudio:LStopNode(TheClient.delayID[4])
                            end
                        end,
    InitialiseAudio = function(soundEngine)
                        TheClient.Print("Initialising Sound engine...")
                        TheClient.myAudio = soundEngine
                        --frames per buffer, channels, sample rate -- deley(secs), maxsounds
                        TheClient.myAudio:LInitialiseEngine(4,2,44100,2,256)
                        local source = 
                        {
                            path = "Sounds/",
                            name = "Play.wav",
                            volume = 1.0,
                        }
                        TheClient.myAudio:LCreateSource(source)
                        source.name = "noo.wav"
                        TheClient.myAudio:LCreateSource(source)
                        source.name = "shot.wav"
                        TheClient.myAudio:LCreateSource(source)
                        source.name = "bikeEngine.wav"
                        TheClient.myAudio:LCreateSource(source)

                        TheClient.Print("Sound engine Ready!...")
                    end,

    Start =         function(game)
                        TheClient.myGame = game
                        TheClient.musicID = -1
                        TheClient.positionMusicID = -1
                        TheClient.positionNodeID = -1
                        TheClient.InitialiseGame()
                    end
}